﻿using HECore.Tenant.Contract;
using HECore.Tenant.Contract.Models.Emoloyees.Dtos;
using HECore.Tenant.Contract.Models.Emoloyees.Inputs;
using HECore.Tenant.Repository.Context;
using HECore.Tenant.Repository.Entites;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HECore.Tenant.Service
{
    public class EmployeeService : IEmployeeService
    {
        private readonly TenantContext _tenantContext;
        public EmployeeService(TenantContext tenantContext) { _tenantContext = tenantContext; }

        public async Task<List<EmployeeDetailsDto>> GetAccountEmoloyees(Guid AccountId)
        {
            var Q = from Employee in _tenantContext.Employee.Where(x => x.AccountId == AccountId && x.IsDelete == false)
                    join Tenant in _tenantContext.Tenant.Where(x => x.IsDelete == false)
                    on Employee.TenantId equals Tenant.Id
                    select new EmployeeDetailsDto()
                    {
                        AccountId = Employee.AccountId,
                        EmployeeEmail = Employee.EmployeeEmail,
                        EmployeeFullName = Employee.EmployeeFullName,
                        EmployeeState = Employee.EmployeeState,
                        CreationTime = Employee.CreationTime,
                        Id = Employee.Id,
                        TenantId = Employee.TenantId,
                        TenantLogo = Tenant.Logo,
                        TenantName = Tenant.Name,
                    };
            var Emoloyees = await Q.OrderByDescending(x => x.CreationTime).ToListAsync();
            return Emoloyees;
        }

        public async Task<EmployeePermissionsDto> GetEmployeePermissions(Guid EmployeeId, Guid TenantId)
        {
            var q = from er in _tenantContext.EmoloyeeRole.AsNoTracking().Where(x => x.TenantId == TenantId && x.IsDelete == false)
                    join role in _tenantContext.Role.AsNoTracking().Where(x => x.TenantId == TenantId && x.IsDelete == false)
                    on er.RoleId equals role.Id
                    where er.EmployeeId == EmployeeId
                    select role;
            var roles = await q.ToListAsync();
            var dto = new EmployeePermissionsDto() { IsAdministrator = false, Permissions = new List<string>() };
            if (roles.Any())
            {
                dto.IsAdministrator = roles.Where(x => x.SystemRoleType == Contract.Enums.SystemRoleTypeEnum.Administrator).Count() > 0;
                if (!dto.IsAdministrator)
                {
                    var roleIds = roles.Select(x => x.Id).Distinct().ToList();
                    dto.Permissions = await _tenantContext.RolePermission.AsNoTracking().Where(x => x.TenantId == TenantId && roleIds.Contains(x.RoleId)).Select(x => x.FunctionName).Distinct().ToListAsync();
                }
            }
            return dto;
        }

        public async Task SetToken(SetTokenInput input)
        {
            _tenantContext.EmployeeToken.Add(new EmployeeToken()
            {
                CreationTime = DateTime.Now,
                CreatorEmployeeId = input.EmployeeId,
                Id = Guid.NewGuid(),
                IP = input.IP,
                RefreshToken = input.RefreshToken,
                RefreshTokenExpireTime = input.RefreshTokenExpireTime,
                TenantId = input.TenantId,
                Token = input.Token,
                TokenExpireTime = input.TokenExpireTime
            });
            await _tenantContext.SaveChangesAsync();
        }
    }
}
